package com.lening.gateway.web;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

public final class Request {
    /**
     * get请求异步调用
     * @param uri
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> Mono<T> asyncGet(String uri, Class<T> clazz) {
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.get().uri("http://" + uri).retrieve().bodyToMono(clazz);
    }
    /**
     * get请求同步步调用
     *
     * @param uri
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T syncGet(String uri, Class<T> clazz) {
        RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);
        return restTemplate.getForObject("http://" + uri, clazz);
    }

    /**
     * post请求异步调用
     * @param uri
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> Mono<T> asyncPost(String uri,Object params, Class<T> clazz) {
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.post().uri("http://" + uri, params).body(BodyInserters.fromValue(params)).retrieve().bodyToMono(clazz);

    }

    /**
     * post请求同步调用
     * @param uri
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T syncPost(String uri,Object params, Class<T> clazz) {
        RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);
        return restTemplate.postForObject("http://" + uri, params, clazz);
    }

    public static <T> Mono<T> asyncPosts(String uri,Object params, Class<T> clazz) {
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.post().uri("http://" + uri, params).body(BodyInserters.fromValue(params)).retrieve().bodyToMono(clazz);

    }

    /**
     * put请求异步调用
     * @param uri
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> Mono<T> asyncPut(String uri,Object params, Class<T> clazz) {
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.put().uri("http://" + uri, params).body(BodyInserters.fromValue(params)).retrieve().bodyToMono(clazz);
    }

    /**
     * put请求同步调用
     * @param uri
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T syncPut(String uri,Object params, Class<T> clazz) {
        RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);
        HttpEntity<Object> httpEntity = new HttpEntity<>(params);
        return restTemplate.exchange("http://" + uri, HttpMethod.PUT, httpEntity, clazz).getBody();
    }

    /**
     * delete请求异步调用
     * @param uri
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> Mono<T> asyncDelete(String uri,Object params, Class<T> clazz) {
        WebClient webClient = SpringUtil.getBean(WebClient.class);
        return webClient.delete().uri("http://" + uri,params).retrieve().bodyToMono(clazz);
    }

    /**
     * delete请求同步调用
     * @param uri
     * @param params
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T syncDelete(String uri,Object params, Class<T> clazz) {
        RestTemplate restTemplate = SpringUtil.getBean(RestTemplate.class);
        HttpEntity<Object> httpEntity = new HttpEntity<>(params);
        return restTemplate.exchange("http://" + uri, HttpMethod.DELETE, httpEntity, clazz).getBody();
    }
}
